let app = getApp()
const util = require('../../../utils/util.js')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        btnDisable: false,
        spinShow: false,
        customerId: null,
        receiver: "",
        rphone: "",
        rarea: "",
        raddress: "",
        birthday: "",
        remark: "",
        fileList: [],
        picUrl: null,
        //日期组件
        show: false,
        //销售单选择客户界面
        orderChoose: false,
        //错误提示
        receiverErrorMessage: "",
        rareaErrorMessage: ""
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let customerId = options.customerId
        if (customerId) {
            this.setData({
                customerId: customerId
            })
            this.queryData()
        }
        //判断是否在销售单选择客户界面
        let orderChoose = options.orderChoose
        if (orderChoose) {
            this.setData({
                orderChoose: orderChoose
            })
        }
    },
    onShow: function () {
    },
    queryData: function () {
        let _this = this
        app.formGet('/app/customer/info?id=' + this.data.customerId, null).then(res => {
            _this.setData({
                spinShow: false
            });
            if (res.data) {
                if (res.data.avatarUrl) {
                    let imageObj = {}
                    let tempFileList = []
                    let picUrl = res.data.avatarUrl
                    imageObj.url = picUrl
                    imageObj.isImage = true
                    tempFileList.push(imageObj)
                    _this.setData({
                        fileList: tempFileList,
                        picUrl: picUrl,
                    })
                }
                _this.setData({
                    receiver: res.data.receiver,
                    rphone: res.data.rphone,
                    rarea: res.data.rarea,
                    raddress: res.data.raddress,
                    birthday: res.data.birthday,
                    remark: res.data.remark
                });
            }

        }).catch(e => {
            _this.setData({
                spinShow: false
            });
            app.message(e, 'error')
        })
    },
    handleEditCustomer() {
        if (this.data.receiver == '') {
            this.setData({
                receiverErrorMessage: "收货人不能为空"
            })
            return false;
        }
        if (this.data.rarea == '') {
            this.setData({
                rareaErrorMessage: "收货人地区不能为空"
            })
            return false;
        }
        let _this = this
        let customerData = {}
        customerData.id = this.data.customerId
        customerData.avatarUrl = this.data.picUrl
        customerData.receiver = this.data.receiver
        customerData.rphone = this.data.rphone
        customerData.rarea = this.data.rarea
        customerData.raddress = this.data.raddress
        customerData.remark = this.data.remark
        _this.setData({
            btnDisable: true
        })
        let url = "/app/customer/add"
        if (this.data.customerId != null) {
            url = "/app/customer/edit"
        }
        app.formPost(url, customerData).then(res => {
            _this.setData({
                spinShow: false
            });
            if (res.code === 0) {
                app.message(res.message, 'success')
                if (_this.data.orderChoose) {
                    let pages = getCurrentPages();
                    let orderPage = pages[pages.length - 3];
                    console.log("res.data %o", res.data)
                    orderPage.setData({
                        customerId: res.data,
                        customerName: _this.data.receiver + "(" + _this.data.rarea + ")"
                    })
                    wx.navigateBack({
                        delta: 2,
                    })
                } else {
                    setTimeout(function () {
                        wx.navigateBack({
                            delta: 1
                        })
                    }, 2000)
                }
            } else {
                app.message(res.message, 'error')
            }
            _this.setData({
                btnDisable: false
            })
        }).catch(e => {
            _this.setData({
                spinShow: false
            });
            _this.setData({
                btnDisable: false
            })
            app.message(e, 'error')
        })
    },
    picAfterRead(event) {
        const {
            file
        } = event.detail;
        // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
        let _this = this
        wx.uploadFile({
            url: app.globalData.baseAPI + '/app/upload/pic', // 仅为示例，非真实的接口地址
            filePath: file.url,
            name: 'file',
            formData: {},
            header: {
                'Authorization': wx.getStorageSync('token')
            },
            success(res) {
                // 上传完成需要更新 fileList
                let result = JSON.parse(res.data)
                if (result.code === 0) {
                    let imageObj = {}
                    let tempFileList = []
                    imageObj.url = result.data
                    imageObj.isImage = true
                    tempFileList.push(imageObj)
                    _this.setData({
                        fileList: tempFileList,
                        picUrl: result.data
                    })
                }
            },
        });
    },
    deleteFile: function (e) {
        let index = e.detail.index
        let fileList = this.data.fileList
        fileList.splice(index, 1)
        this.setData({
            fileList: fileList
        })
    },
    onDisplay() {
        this.setData({show: true});
    },
    onClose() {
        console.log("close")
        this.setData({show: false});
    },
    onConfirm(event) {
        console.log("confirm")
        this.setData({
            show: false,
            birthday: util.formatDate(event.detail),
        });
    },
})